package com.hyt.it.ogt.ks.mq;

import com.alibaba.fastjson.JSON;
import com.hyt.it.ogt.ks.service.ICandidateLogOtherService;
import com.hyt.it.ogt.opertaion.track.model.OtherLogVO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 其它轨迹日志记录保存
 *
 * @author 朱振宇
 * @since 2022-12-02
 */
@Component
@Slf4j
public class KsOperationTrackOtherLogSaveMqConsumer {
    @Resource
    private ICandidateLogOtherService candidateLogOtherService;

    @RabbitListener(bindings = {@QueueBinding(value = @Queue( value = "${mq.operationtrackdirect.ksOperationTrackOtherQueue}",
            autoDelete = "false",durable = "true"),
            exchange = @Exchange( value = "${mq.operationtrackdirect.ksOperationTrackOtherExchange}" ,type = ExchangeTypes.DIRECT),
            key = {"${mq.operationtrackdirect.ksOperationTrackOtherRoute}"})})
    public void ksOperationTrackListener(String json, Message message, Channel channel) throws IOException {
        log.info("KsOperationTrackOtherLogSaveMqConsumer消费队列消息: {}", json);
        try {
            List<OtherLogVO> otherLogVOList = JSON.parseArray(json, OtherLogVO.class);
            //保存其它轨迹日志
            candidateLogOtherService.saveOtherLog(otherLogVOList);
            //同步考生其它轨迹状态
            candidateLogOtherService.saveCandidateOtherStatus(otherLogVOList);
        } catch (Exception e) {
            log.error("ks其它轨迹日志记录保存异常: 【{}】------{}", json, e);
            /*
             * 拒绝确认消息:<br>
             * channel.basicNack(long deliveryTag, boolean multiple, boolean requeue) ; <br>
             * deliveryTag:该消息的index<br>
             * multiple：是否批量.true:将一次性拒绝所有小于deliveryTag的消息。<br>
             * requeue：被拒绝的是否重新入队列 <br>
             */
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
